package com.zline.foodraise.db.dao;

import android.util.SparseArray;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;
import com.zline.foodraise.db.DatabaseHelper;
import com.zline.foodraise.entity.Province;

import org.androidannotations.annotations.EBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Created by heshantao on 15/7/10.
 * 省份dao
 */
@EBean
public class ProvinceDao {
    private static final String LOG_TAG = ProvinceDao.class.getSimpleName();
    //Integer 代表主键类型
    Dao<Province, Integer> provinceDao;
    DatabaseHelper helper;
    public void setHelper(DatabaseHelper helper) {
        this.helper = helper;
    }

    void getInstance() {
        try {
            if (provinceDao == null) {
                provinceDao = helper.getDao(Province.class);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //添加省份
    public void add(final Province province) {
        try {
            helper.getWritableDatabase();
            getInstance();
            TransactionManager.callInTransaction(helper.getConnectionSource(), new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    provinceDao.create(province);
                    return null;
                }
            });

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //查询所有省份
    public List<Province> findAll(){
        helper.getReadableDatabase();
        getInstance();
        List<Province> list = new ArrayList<Province>();
        try {
            list=provinceDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


}
